import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import {
  create,
  NAvatar,
  NInput,
  NIcon,
  NTag,
  NDivider,
  NButton,
  NDrawer,
  NCard,
  NTabs,
  NTabPane,
  NSwitch,
  NModal,
  NBadge,
  NPopover,
  NImage,
  NPopconfirm
} from 'naive-ui'

import {
  ElUpload,
  ElButton,
  ElRadioGroup,
  ElRadioButton,
  ElRadio,
  ElDialog,
  ElInput,
  ElMessage,
  ElDropdown
} from 'element-plus'
import 'element-plus/dist/index.css'

import http from './utils/request'
import common from './utils/common'
import constant from './utils/constant'

// import 'virtual:svg-icons-register'
import SvgIcon from '@/components/SvgIcon'
import elementIcons from '@/components/SvgIcon/svgicon'

import 'vfonts/FiraCode.css'
import './assets/css/index.css'
import './assets/css/color.css'
import './assets/css/animation.css'
import * as net from 'net'

const naive = create({
  components: [NAvatar, NInput, NIcon, NTag, NDivider, NButton,
    NDrawer, NCard, NTabs, NTabPane, NSwitch, NModal, NBadge,
    NPopover, NImage, NPopconfirm]
})

const app = createApp(App)
app.use(router)
app.use(store)
app.use(naive)
app.use(elementIcons)

app.component(ElUpload.name, ElUpload)
app.component(ElButton.name, ElButton)
app.component(ElRadioGroup.name, ElRadioGroup)
app.component(ElRadio.name, ElRadio)
app.component(ElRadioButton.name, ElRadioButton)
app.component(ElDialog.name, ElDialog)
app.component(ElInput.name, ElInput)
app.component(ElDropdown.name, ElDropdown)
app.component('SvgIcon', SvgIcon)

app.config.globalProperties.$http = http
app.config.globalProperties.$common = common
app.config.globalProperties.$constant = constant

const whiteList = ['/login', '/auth-redirect', '/register'] // 设置白名单

/*
* 当要路由到 chat 界面
*
*
* */

router.beforeEach((to, from, next) => {
  // 1. 要路由的界面不是登录界面，需要检查是否有 token, 如果有 token 直接放行，否则就将其路由到登录界面
  // 2. 直接跳转到登录
  // 看有没有token
  const token = localStorage.getItem('userToken')
  const defaultStoreType = localStorage.getItem('defaultStoreType')
  if (defaultStoreType === 'undefined') {
    localStorage.setItem('defaultStoreType', 'qiniu')
  }
  if (token) {
    // token 存在表示用户已经登录了就放行
    if (to.path === '/login') {
      next({ path: '/chat' })
    } else {
      next()
    }
  } else {
    // 路由到 登录界面
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      ElMessage.warning('请先进行登录')
      next(`/login?redirect=${to.path}`)
    }
  }

  /* if (to.meta.requiresAuth) {
    if (to.path === '/chat') {
      // 判断 to.query.defaultStoreType 是否被定义，如果被定义就存储到 localStorage 中去
      if (typeof to.query.defaultStoreType !== 'undefined') {
        localStorage.setItem('defaultStoreType', to.query.defaultStoreType)
      }
      // 用户的 token 存在 发送 http 请求 使用 token 进行登录
      if (typeof to.query.userToken !== 'undefined') {
        const userToken = to.query.userToken
        const xhr = new XMLHttpRequest()
        xhr.open('post', constant.baseURL + '/user/token', false)
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        xhr.send('userToken=' + userToken)
        const result = JSON.parse(xhr.responseText)
        if (!common.isEmpty(result) && result.code === 200) {
          store.commit('loadCurrentUser', result.data)
          localStorage.setItem('userToken', result.data.accessToken)
          window.location.href = constant.imURL
          next()
        } else {
          window.location.href = constant.webBaseURL
        }
      } else if ((localStorage.getItem('userToken'))) {
        // 如果本地存储的有 token 就直接放行
        next()
      } else {
        // 跳转到基础网页界面
        window.location.href = constant.webBaseURL
      }
    } else {
      if ((localStorage.getItem('userToken'))) {
        next()
      } else {
        window.location.href = constant.webBaseURL
      }
    }
  } else {
    next()
  } */
})

app.mount('#app')
